package edu.ts.crowdfunding.category.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ts.crowdfunding.category.domain.Category;
import edu.ts.crowdfunding.category.domain.ItemCountInCategory;
import edu.ts.crowdfunding.category.domain.PO.CategoryConditionalQueryPO;
import edu.ts.crowdfunding.category.domain.PO.CategoryUpdatePO;
import edu.ts.crowdfunding.category.domain.VO.CategoryConditionalQueryVO;
import edu.ts.crowdfunding.category.service.CategoryService;
import edu.ts.crowdfunding.category.mapper.CategoryMapper;
import edu.ts.crowdfunding.product.mapper.ProductMapper;
import edu.ts.crowdfunding.product.service.ProductService;
import jdk.jfr.TransitionFrom;
import org.springframework.beans.factory.annotation.Autowired;
import edu.ts.crowdfunding.category.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import edu.ts.crowdfunding.category.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper CategoryMapper;
    @Autowired
    private ProductService productService;

    @Transactional
    @Override
    public Result deleteWithProducts(Long id) {
         //先删除商品
         productService.deleteProductById(id);
         CategoryMapper.deleteWithProducts(id);
         return new Result<>(1,"删除成功",null);
    }
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result pageSearch(CategoryConditionalQueryPO categoryConditionalQueryPO) {
        Integer pageNum = categoryConditionalQueryPO.getPageNum();
        Integer pageSize = categoryConditionalQueryPO.getPageSize();
        //默认页号
        if (pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        //默认页大小
        if (pageSize == null || pageSize < 1){
            pageSize = 10;
        }
        try {
            //设置分页
            PageHelper.startPage(pageNum,pageSize);
            //查询分类列表
            List<Category> list = categoryMapper.pageSearch(categoryConditionalQueryPO);
            //查询每个分类下的项目数
            List<ItemCountInCategory> itemCountInCategory = categoryMapper.getItemCoutInCategory();
            //最终的分类结构列表
            List<CategoryConditionalQueryVO> categoryConditionalQueryVOS = new ArrayList<>();
            //遍历原始分类列表
            for (Category category : list) {
                //构建分类列表结构(原始分类结构+分类下项目数)
                CategoryConditionalQueryVO categoryConditionalQueryVO = new CategoryConditionalQueryVO();
                BeanUtils.copyProperties(category,categoryConditionalQueryVO);
                //遍历项目数列表
                for (ItemCountInCategory singleItemCountInCategory : itemCountInCategory){
                    //如果id一致则将项目数赋值到最终的对象中
                    if (singleItemCountInCategory.getCategoryId().equals(category.getCategoryId())){
                        categoryConditionalQueryVO.setProjectCount(singleItemCountInCategory.getItemCount());
                    }
                }
                //对没有被赋值的projectCount默认为分类下没有商品
                if (categoryConditionalQueryVO.getProjectCount() == null){
                    categoryConditionalQueryVO.setProjectCount(0);
                }
                //添加到最终集合中
                categoryConditionalQueryVOS.add(categoryConditionalQueryVO);
            }
            //构建结果集
            PageInfo<Category> pageInfo = new PageInfo<>(categoryConditionalQueryVOS);
            return new Result(1,"查询成功",pageInfo);
        } catch (Exception e) {
            return new Result(0,"查询失败: "+e.getMessage(),null);
        }
    }

    @Override
    public Result updateCategory(CategoryUpdatePO categoryUpdatePO) {
        if (categoryUpdatePO.getCategoryName() == null || categoryUpdatePO.getCategoryName().isEmpty()){
            return new Result(0,"分类名称不能为空",null);
        }
        if (categoryUpdatePO.getCategorySortOrder() == null){
            return new Result(0,"排序不能为空",null);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdatePO, category);
        category.setCategoryUpdatedAt(new Date());
        categoryMapper.updateById(category);
        return new Result(1,"修改成功",null);
    }

    @Override
    public Result updateStatus(Long id,Integer status) {
        if (status == null){
            return new Result(0,"状态不能为空",null);
        }
        Category category = new Category();
        category.setCategoryId(id);
        //设置状态
        category.setStatus(status);
        categoryMapper.updateById(category);
        return new Result(1,"修改成功",status);
    }

}




